<script lang="ts" setup>
import type { VxeGridListeners, VxeGridProps } from '#/adapter/vxe-table';

import { onMounted, reactive, ref } from 'vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { kickoutByTokenValue, onlineDevice } from '#/api/sys/user';
import { TableAction } from '#/components/table-action';

const requestData = () => {
  return new Promise((resolve) => {
    onlineDevice().then((res) => {
      resolve({
        total: res.length,
        items: res,
      });
    });
  });
};
const gridOptions = reactive<VxeGridProps<any>>({
  toolbarConfig: {
    enabled: true,
    refresh: true,
    print: false,
    export: false,
    custom: true,
    zoom: true,
    slots: {
      buttons: 'toolbar-buttons',
    },
  },
  columns: [
    { field: 'deviceInfo', title: '登录设备' },
    { field: 'username', title: '登录账号' },
    { field: 'browserInfo', title: '浏览器' },
    { field: 'clientIp', title: '登录IP' },
    { field: 'loginTime', title: '登录时间' },
    {
      width: 100,
      title: '操作',
      align: 'center',
      slots: { default: 'ACTION' },
      fixed: 'right',
    },
  ],
  data: [],
  border: false,
  height: '330',
  keepSource: true,
  pagerConfig: {
    enabled: false,
    pageSize: 5,
  },
  proxyConfig: {
    ajax: {
      query: async () => {
        return await requestData();
      },
    },
  },
});
const gridEvents: VxeGridListeners<any> = {};
const [Grid, GridApi] = useVbenVxeGrid({
  gridOptions,
  gridEvents,
});
onMounted(() => {
  requestData();
});
const handleDelete = (row: any) => {
  kickoutByTokenValue({
    loginId: row.loginId,
  }).then(() => {
    GridApi.reload();
  });
};
</script>
<template>
  <div>
    <Grid>
      <template #toolbar-buttons> 我的在线设备 </template>
      <template #ACTION="{ row }">
        <TableAction
          :actions="[
            {
              label: '踢下线',
              icon: 'ant-design:delete-outlined',
              popConfirm: {
                title: '确定踢下线吗？',
                confirm: handleDelete.bind(null, row),
              },
            },
          ]"
        />
      </template>
    </Grid>
  </div>
</template>
<style lang="less" scoped></style>
